<?
include("common/header.html");
include("common/left.html");
include("common/page.php");
include("class/Member.php");
$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
$memberID   =$_REQUEST['id'];
$st="select account from pft_member where id='$memberID'";
$GLOBALS['le']->query($st);
$GLOBALS['le']->fetch_assoc();
$accounts=$GLOBALS['le']->f('account');
$m          = new Member($s,$memberID);
$salePros   = $m->salePros();

if($_REQUEST['ltitle']){
    $saveArr=array();
    // 数值表示按照门票 ID 
    if(is_numeric($_REQUEST['ltitle'])){
        foreach($salePros as $k=>$v){
            if($v['tid']==$_REQUEST['ltitle']) $saveArr[]=$v;
        }
    }else{
        $tmpArr = array();
        $ltitle = trim($_REQUEST['ltitle']);
        $str="SELECT id FROM uu_land WHERE title LIKE '%$ltitle%'";
        $GLOBALS['le']->query($str);
        while($row=$GLOBALS['le']->fetch_assoc()){
            $tmpArr[]=$row['id'];
        }
        foreach($salePros as $k=>$v){
            if(in_array($v['lid'], $tmpArr)) $saveArr[]=$v;
        }
    }


    $salePros=$saveArr;
}
//print_r($salePros[0]);
//$str="select * from pft_member where id=$memberID";
//$GLOBALS['le']->query($str);
//$m_info = $GLOBALS['le']->fetch_assoc();

//景区相关
$p_typeT=array("A"=>"景点门票","B"=>"旅游线路","C"=>"酒店客房","D"=>"旅游商品","F"=>"套票产品","G"=>"餐饮产品");

$p_config=array("A"=>array("unit"=>"张"),"B"=>array("unit"=>"张"),"C"=>array("unit"=>"间"),"D"=>array("unit"=>"件"));

$p_type=$_REQUEST['p_type']?$_REQUEST['p_type']:"";
$str="select area_id,area_name from uu_area";
$GLOBALS['le']->query($str);
$areas=array();
while($row=$GLOBALS['le']->fetch_assoc()){
    $areas[$row['area_id']]=$row['area_name'];
}
//$s->PFT_Member_Fund_Modify(4,4,100000,0,0);
$str="select m.dname,m.qq,m.id,e.com_name from pft_member m left join pft_member_extinfo e on e.fid=m.id where m.dtype=0";
$GLOBALS['le']->query($str);
$apply_info=array();
while($row=$GLOBALS['le']->fetch_assoc()){
    $apply_info[$row['id']]=array("com_name"=>$row['com_name'],"dname"=>$row['dname'],"qq"=>$row['qq'],"address"=>$row['address']);
}
$hotcitys=array();
$p_types=array();
$province=array();
$citys=array();
$destination=array();
$startplaces=array();
$endplaces=array();
$topics=array();
$lands=array();
$result=array();
$products=array();
foreach($salePros as $row){
    // if(!$p_types[$row['p_type']]&&in_array($row['p_type'],array("A","B","C","D"))){
    // $p_types[$row['p_type']]=$p_typeT[$row['p_type']];
    // }
    if(!$p_types[$row['p_type']]&&in_array($row['p_type'],array("A","B","C","D","F","G"))){
        $p_types[$row['p_type']]=$p_typeT[$row['p_type']];
    }
    // if($row['order_end']!=""){
        // $row['usetime']=$row['order_end'];
    // }else{
        // $row['usetime']=$row['delaydays']>0?"游玩日期起".$row['delaydays']."天内有效":"游玩日期当日有效";
    // }
	// 有效期
	if($row['delaydays']>0){
		if($row['delaytype']==0) $row['usetime'] = '游玩日期(含)'.$row['delaydays'].'天内有效';
		if($row['delaytype']==1) $row['usetime'] = '下单日期(含)'.$row['delaydays'].'天内有效';
	}
	if($row['order_start']!='' && $row['order_end']!=''){
		$row['usetime'] = $row['order_start'].' 至 '.$row['order_end'];
	}elseif($row['order_start']=='' && $row['order_end']!=''){
		$row['usetime'] = '截止'.$row['order_end'];
	}
	
	if($row['order_start']=='' && $row['delaydays'] == 0){
		$row['usetime'] = '当天有效';
	}

    $row['paymethod']=($row['pay']==1)?"在线支付":"现场支付";
    $row['notes']=$tips.$row['notes'];
    if($row['p_type']=="A"){
        $row['getaddr']="取票信息:".$row['getaddr'];
    }elseif($row['p_type']=="B"){
        $ass_station=(array)unserialize($row['ass_station']);
        foreach($ass_station as $key=>$value){
            $row['getaddr']="集合地址:".$value;
            break;
        }
    }
    $area=explode("|",$row['area']);
    /*产品推荐度*/
    $tuijian=0;
    if($row['apply_did']==$memberID){ $tuijian+=100; $row['self']="self"; }
    $row['tuijian']=$tuijian;
    $row['tprice']=str_replace(".00","",$row['tprice']);
    if(!$lands[$row['lid']]){
        $lands[$row['lid']]=array("title"=>$row['title'],
            "self"=>$row['self'],
            "area"=>"[".$areas[$area[1]]."]",
            "dest"=>$areas[$area[1]],
            "taobao_support"=>$row['taobao_support'],
            "imgpath"=>$row['imgpath'],
            "address"=>$row['address'],
            "apply_did"=>$row['apply_did'],
            "terminal"=>$row['terminal'],
            "salerid"=>$row['salerid'],
            "destid"=>$area[1]);
    }
    if(!$lands[$row['lid']]['lowprice']){
        // $lands[$row['lid']]['lowprice']=($row['dprice']+$row['jsprice']);
        $lands[$row['lid']]['lowprice']=($row['jsprice']);
    }
    // if($lands[$row['lid']]['lowprice']>($row['dprice']+$row['jsprice'])){
    if($lands[$row['lid']]['lowprice']>($row['jsprice'])){
        // $lands[$row['lid']]['lowprice']=($row['dprice']+$row['jsprice']);
        $lands[$row['lid']]['lowprice']=($row['jsprice']);
    }
    if($area[1]){$citys[$area[1]]++;}
    //if($p_type!=$row['p_type']) continue;
    if($p_type!='' && $p_type!=$row['p_type']) continue;
    //筛选条件
    if($p_type=="B"){
        $lands[$row['lid']]['class']="route";
        $lands[$row['lid']]['titleCount']=50;
        $places=explode("|",$row['runtime']);
        if($places[0]&&!in_array($places[0],$startplaces)){
            $startplaces[]=$places[0];
        }
        if($places[1]&&!in_array($places[1],$endplaces)){
            $endplaces[]=$places[1];
        }
        if($_REQUEST['startplace']&&$places[0]!=$_REQUEST['startplace']) continue;
        if($_REQUEST['endplace']&&$places[1]!=$_REQUEST['endplace']) continue;
        if($rdays=$_REQUEST['rday']){
            if($rdays<=3&&$row['rdays']!=$rdays) continue;
            if($rdays>3&&$row['rdays']<=3) continue;
        }
    }else{
        if($p_type=="F"){
            $lands[$row['lid']]['class']="route";
        }
        if($area[0]){
            $province[$area[0]]=$areas[$area[0]];
        }
        if($_REQUEST['provinceid']&&!in_array($_REQUEST['provinceid'],$area)) continue;
        if($area[1]){
            $destination[$area[1]]=$areas[$area[1]];
        }
        if($_REQUEST['destid']&&!in_array($_REQUEST['destid'],$area)) continue;
        foreach(explode(",",$row['topic']) as $value){
            if(!in_array($value,$topics)&&$value){
                $topics[]=$value;
            }
        }
        if($_REQUEST['topic']&&strpos($row['topic'],$_REQUEST['topic'])===false) continue;
    }
    if(urldecode($_REQUEST['key'])
        && strpos($row['title'],trim(urldecode($_REQUEST['key'])))===false
        && strpos(trim($_REQUEST['key']),$row['title'])===false) {
        continue;
    }
    //是否有有效门票可售
    $products[]=$row;
}

arsort($citys);
foreach($citys as $k=>$v){
    if($city_i++>=15) break;
    $hotcitys[$k]=mbsubstr($areas[$k],4);
}
ksort($p_types);
//排序
uasort($products,"my_sort");
function my_sort($a,$b){
    $tuijiana=$a['tuijian'];
    $tuijianb=$b['tuijian'];
    if ($tuijiana==$tuijianb){
        $pxa=$a['px'];
        $pxb=$b['px'];
        if($pxa==$pxb) return 0;
        return ($pxa>$pxb) ? -1 : 1;
    }else{
        return ($tuijiana>$tuijianb) ? -1 : 1;
    }
}

//数据处理
//$count0=count($products);
foreach($products as $row){
    $result[$row['sapply_did']][$row['lid']][]=$row;
}

$currentPage    = $_REQUEST['currentPage']?abs($_REQUEST['currentPage']):1;
$pageSize       = $_REQUEST['pageSize']?abs($_REQUEST['pageSize']):10;
$offset         = ($currentPage-1)*$pageSize;
$r=array();
$Y_m_d = date('Y-m-d');
//$account =
$cut = 0;
foreach($result as $sapply_did=>$applylands){
    foreach($applylands as $lid=>$row){            
        $price_flag = 0;        
        foreach ($row as $k=>$p){
            $price = getPrice( $s, $accounts, $p['pid'], $Y_m_d, $sapply_did);
            //print_r($price);
            if ($price==-1) {
                $cut += 1;
//                unset($row[$k]);
//                continue;
            }
            
            $price_flag = 1;
            $row[$k]['uprice']  = $price['uprice'];
            $row[$k]['jsprice'] = $price['jsprice']; 
        }
       // print_r($row);
        
        if (!$price_flag) continue;
        for($j=0;$j<=$k;$j++){
            if($j==$k && !$row[$j]['jsprice']){
                continue 2;
            }
            if(!$row[$j]['jsprice']){
                unset($row[$j]);
                continue;
            }
            break;
        }
        $count++;
        if($offset>=$count||$count>($offset+$pageSize)) continue;
        $r[$sapply_did][$lid] = $row;
    }
}

//print_r($r);
// exit;
// $count = $i ;
function getPrice($s, $accounts, $pid, $Y_m_d, $sapply_did) {
    $price = array();
    $xml = $s->Dynamic_Price_And_Storage($accounts,$pid,$Y_m_d,1,0,0,$sapply_did);
    if($_GET['z']) {echo $accounts.','.$pid.','.$Y_m_d.',1,0,0,'.$sapply_did;
    echo '_';
    print_r($xml);
    exit;
    }
    if($xml==105){
        $xml = $s->Dynamic_Price_And_Storage($accounts,$pid,$Y_m_d,2,0,0,$sapply_did);
        if ($xml==105) {
            return -1;
        }
    }
    elseif ($xml==101 || $xml==1065 || $xml==103) {
        return -1;
    }

    $price['jsprice']=(int)simplexml_load_string($xml)->Rec->UUsprice;

    //从接口获取零售价
    $xml = $s->Dynamic_Price_And_Storage($accounts,$pid,$Y_m_d,1,1,0,$sapply_did);
    if($xml==105){
        $xml = $s->Dynamic_Price_And_Storage($accounts,$pid,$Y_m_d,2,1,0,$sapply_did);
    }
    elseif ($xml==101 || $xml==1065 || $xml==103) {
        return -1;
    }
    $price['uprice']=(int)simplexml_load_string($xml)->Rec->UUsprice;
    return $price;
}
include("tpl/saleList.html");
include("common/footer.html");
?>